package com.ssdp.dto;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.http.HttpStatus;

import java.util.List;
import java.util.Objects;

/**
 * @author syo 2319615164@qq.com
 * @description 通用结果对象
 * @date 2024/3/14
 * @Copyright syo
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
@SuppressWarnings("unused")
public class Result {

    /**
     * 是否成功
     */
    private Boolean success;

    /**
     * 响应码
     */
    private Integer code;

    /**
     * 错误信息
     */
    private String msg;

    /**
     * 携带的数据
     */
    private Object data;

    /**
     * 数据列表的数据长度
     */
    private Long total;

    public static Result ok(){
        return new Result(true, 200, "请求成功", null, 0L);
    }

    public static Result ok(Object data){
        return new Result(true, 200,  "请求成功", data, 1L);
    }

    public static  Result ok(List<?> data, Long total){
        return new Result(true, 200, "请求成功", data, total);
    }

    public static Result fail(String msg){
        return new Result(false, -1, msg, null, null);
    }

    public static Result buildOk(HttpStatus status, Object data, Long total){
        return new Result(true, status.getCode(), status.getDesc(), data, total);
    }


    public static Result buildOk(HttpStatus status, List<?> data, Long total){
        return new Result(true, status.getCode(), status.getDesc(), data, total);
    }

    public static Result buildFail(HttpStatus status, Object data, Long total){
        return new Result(false, status.getCode(), status.getDesc(), data, total);
    }

    @SuppressWarnings("all")
    public static enum HttpStatus {
        SUCCESS_STATUS(200, "请求成功"),
        FAIL_STATUS(-1, "请求失败"),
        AUTHENTICATE_FAIL(401, "认证失败");

        private Integer code;
        private String desc;
        HttpStatus(Integer code, String desc) {
            this.code = code;
            this.desc = desc;
        }

        public Integer getCode() {
            return code;
        }

        public void setCode(Integer code) {
            this.code = code;
        }

        public String getDesc() {
            return desc;
        }

        public void setDesc(String desc) {
            this.desc = desc;
        }
    }
}
